#include "LostRoutesMenu.h"
#include "Classes/SystemHeader.h"
#include "cocos/audio/include/AudioEngine.h"
USING_NS_CC;

cocos2d::Scene * LostRoutesMenu::createScene()
{
    return LostRoutesMenu::create();
}

bool LostRoutesMenu::init()
{
    if (!Scene::init())
    {
        return false;
    }
    cocos2d::Size visibleSize = Director::getInstance()->getVisibleSize();

    auto bg = TMXTiledMap::create("map/red_bg.tmx");
    addChild(bg);

    //top 
    auto top = Sprite::createWithSpriteFrameName("home-top.png");
    top->setPosition(Vec2(visibleSize.width / 2, visibleSize.height - top->getContentSize().height / 2));
    addChild(top);

    //start Menu
    auto startSpriteNormal   = Sprite::createWithSpriteFrameName("home.button.start.png");
    auto startSpriteSelected = Sprite::createWithSpriteFrameName("home.button.start-on.png");

    auto startMenuItem = MenuItemSprite::create(
        startSpriteNormal, 
        startSpriteSelected,
        CC_CALLBACK_1(LostRoutesMenu::menuItemCallBack,this));

    startMenuItem->setTag(ActionType::MenuItemStart);

    //setting menu
    auto settingSpriteNormal = Sprite::createWithSpriteFrameName("home.button.setting.png");
    auto settingSpriteSelected = Sprite::createWithSpriteFrameName("home.button.setting-on.png");

    auto settingMenuItem = MenuItemSprite::create(
        settingSpriteNormal, 
        settingSpriteSelected,
        CC_CALLBACK_1(LostRoutesMenu::menuItemCallBack, this));

    settingMenuItem->setTag(ActionType::MenuItemSetting);

    //help menu
    auto helpSpriteNormal   = Sprite::createWithSpriteFrameName("home.button.help.png");
    auto helpSpriteSelected = Sprite::createWithSpriteFrameName("home.button.help-on.png");
    auto helpMenuItem = MenuItemSprite::create(
        helpSpriteNormal,
        helpSpriteSelected,
        CC_CALLBACK_1(LostRoutesMenu::menuItemCallBack, this));
    helpMenuItem->setTag(ActionType::MenuItemHelp);

    auto menu = Menu::create(startMenuItem, settingMenuItem, helpMenuItem, nullptr);
    menu->setPosition(visibleSize.width / 2, visibleSize.height / 2);
    menu->alignItemsVerticallyWithPadding(12);
    addChild(menu);

    return true;
}

void LostRoutesMenu::onEnterTransitionDidFinish()
{
    Scene::onEnterTransitionDidFinish();
    UserDefault *defaults = UserDefault::getInstance();
    if (defaults->getBoolForKey(MusicKey.c_str()))
    {
        cocos2d::experimental::AudioEngine::play2d(bg_music_game, true);
    }


}

void LostRoutesMenu::menuItemCallBack(cocos2d::Ref * pSender)
{
    auto pMenuItem = static_cast<MenuItem *>(pSender);
    auto tag = pMenuItem->getTag();
    CCLOG("tag-->%d", tag);
    switch (tag)
    {
    case  MenuItemStart:
        break;
    case  MenuItemSetting:
        break;
    case  MenuItemHelp:
        break;
    default:
        break;
    }
}
